const fs = require('fs')
const request = require('request')
const cheerio = require('cheerio')
const mysql = require('mysql')
const dateformat = require('dateformat')

// 读取配置信息（网址，需要的段落特征，数据条数等）
const path = './config'

// 初始化数据库连接对象
const db = mysql.createConnection({
  host: 'localhost',
  user: 'root',
  password: 'root',
  database:'bailongtan'
})

db.connect(function(err) {
  console.log(err || '连接成功')
})

// 关闭连接
function close() {
  db.end(function(err) {
    if(!err)
      console.log('关闭成功')
    else
      console.log('关闭失败')
  })
}





// 获取的信息的模板
var templates = {
  title: '',
  time: '',
  url: '',
  type: '',
  info: '首页',
}

// 记录写入数
var successCount = 0

// 错误信息记录
var wrong = []

var conf = fs.readFileSync(path + '/conf.json').toString()
// 获得配置所需的对象
conf = JSON.parse(conf)

// 请求对象（首页）
conf.forEach(function (x, index) {
  if (!x.host) {
    stopRun()
    return
  }
  request(x.url, function (error, response, body) {
    if (!error && response.statusCode === 200) {
      // 开始获取数据
      textTemp = body
      // 初始化$ 对象
      var $ = cheerio.load(textTemp)
      // 按照层级选择对象
      var box = $(x.pix)
      console.log(box.toString())
      box.find('li').each(function (item){
        var info = $(this)
        var span = $(info.find('span'))
        var a = $(info.find('a'))
        // 开始取出数据，构造template
        template = {
          title: a.text(),
          time: span.text(),
          url: x.host + a.attr('href'),
          type: x.type,
          info: x.info,
        }
        if(x.url === 'http://km.wenming.cn/kmyw/') {
          console.log('')
          template.url = x.host + a.attr('href').replace('./', '')
        }
        if(template.title === '') {
          wrong.push('没有获取到标题' + x.url)
          return
        }
        // save
        save(template)
      })
    } else {
      // 做出相应的记录
      wrong.push('打开网址失败' + x.url)
      concole.log('打开网址失败')
    }
  })
})

// 公益广告
request('http://km.wenming.cn/zhuanti/201704qgwm/', function(err, response, body) {
  var $ = cheerio.load(body)
  var gg = $('.gygg_cn')
  gg.find('img').each(function (item) {
    var info = $(this)
    console.log($(info).attr('src'))
    var title = $(info).attr('src').replace('./', 'http://km.wenming.cn/zhuanti/201704qgwm/')
    var templates = {
      title: title,
      time: String(new Date().getTime()),
      url: title,
      type: '图片',
      info: 'gongyitupian',
    }
    save(templates)
  })
})

// 文明城市
const wenming = {
  host: 'http://xw.kunming.cn',
  path: 'http://xw.kunming.cn/zt/node_60620.htm',
}

// 取页面
request(wenming.path, function(err, response, body) {
  var $ = cheerio.load(body)

  // box 1
  var hot_left = $('.box1').html().toString()
  var _hot_left = hot_left.replace(/href="../g, 'href="'+wenming.host)
  _hot_left = _hot_left.replace(/<img src="/g, '<img src="' + wenming.host + '/zt/')
  var templates = {
    title: _hot_left,
    time: String(new Date()),
    url: 'wenming_box1',
    type: 'fe',
    info: 'box1',
  }
  db.query('update bailongtan_ext_pachong set title = ?, time = ?, type = ? where info = ? and url = ?',
    [templates.title, templates.time, templates.type, templates.info, templates.url], function(err, rows, fields) {
      if (err) {
        wrong.push('保存失败' + templates.title)
      }
      else
        successCount += 1
    })

  // box 2
  var box2 = $('.box2').html().toString()
  var _box2 = box2.replace(/href="../g, 'href="'+wenming.host)
  _box2 = _box2.replace(/<img src="/g, '<img src="' + wenming.host + '/zt/')
  _box2 = _box2.replace(/href="node_/g, 'href="'+wenming.host+'/zt/node_')
  var data = {
    title: _box2,
    time: String(new Date()),
    url: 'wenming_box2',
    type: 'box2',
    info: 'box2',
  }

  db.query('update bailongtan_ext_pachong set title = ?, time = ?, type = ? where info = ? and url = ?',
    [data.title, data.time, data.type, data.info, data.url], function(err, rows, fields) {
      if (err) {
        wrong.push('保存失败' + data.title)
      }
      else
        successCount += 1
    })

  // box 4
  var box4 = $('.box4').html().toString()
  var _box4 = box4.replace(/href="node_/g, 'href="'+wenming.host+'/zt/node_')
  var box_data = {
    title: _box4,
    time: String(new Date()),
    url: 'wenming_img1',
    type: 'img1',
    info: 'img1',
  }
  db.query('update bailongtan_ext_pachong set title = ?, time = ?, type = ? where info = ? and url = ?',
    [box_data.title, box_data.time, box_data.type, box_data.info, box_data.url], function(err, rows, fields) {
      if (err) {
        wrong.push('保存失败' + box_data.title)
      }
      else
        successCount += 1
    })

})

// 保存对象
const save = function (data) {
  if(db.state === 'disconnected') {
    db.connect()
  }
  // 查询
  db.query('SELECT data_id from bailongtan_ext_pachong where url = ?', [data.url], function(err, rows, fields) {
    if (err) {
      wrong.push('查询失败' + data.title)
    } else {
      console.log(rows)
    }
    if (rows.length === 0) {
      // save
      db.query('insert into bailongtan_ext_pachong(title, time, type, url, info) values (?, ?, ?, ?, ?)',
        [data.title, data.time, data.type, data.url, data.info], function(err, rows, fields) {
          if (err)
            wrong.push('保存失败' + data.title)
          else
            successCount += 1
        })
    }
  })

}


// 终止
function stopRun() {
  var times = dateformat(new Date(), 'yyyy年mm月dd日  HH:M:s ')
  if (db.state !== 'disconnected') {
    db.end(function(err) {
      if(err) wrong.push('数据库关闭失败')
    })
  }
  setTimeout(function() {
    fs.appendFile('./log/success.txt', 'success:' + successCount + '        -----  ' + times + '\n\n', function(err) {
      console.log(err || 'log save')
    })
    if (wrong.length !== 0) {
      wrong.push('err_data:' + times)
      fs.appendFile('./log/error.txt', wrong.join('    ----   '), function(err) {
        console.log(err || '保存成功:' + successCount)
      })
    } else {
      console.log('没有错误')
    }
    setTimeout(function () {
      process.exit()
    }, 5000)
  }, 30000)
}